var sha1 = require('sha1');
module.exports = {
	createId: createId,
	verficationCode: verficationCode,
	isMobile: isMobile,
	getSendSMSOpinions: getSendSMSOpinions,
	checkSignature: checkSignature
};
var uuid = require('node-uuid');
function createId() {
	return parseInt(uuid.v4().replace(/-/g, ''), 16).toString(36);
}
function verficationCode() {
	if ('test' == process.env.NODE_ENV)
		return '123456';
	var number = Math.floor(Math.random() * 1000000).toString();
	var length = number.length;
	switch (length) {
		case 1:
			number = '00000' + number;
			break;
		case 2:
			number = '0000' + number;
			break;
		case 3:
			number = '000' + number;
			break;
		case 4:
			number = '00' + number;
			break;
		case 5:
			number = '0' + number;
			break;
	}
	return number;
}
function isMobile(mobile) {
	if (/^((13[0-9])|(15[^4,\D])|(18[0-9])|(17[0,6-8]))\d{8}$/.test(mobile)) {
		return true;
	} else {
		return false;
	}
}
function getSendSMSOpinions(message, phones) {
	if (!isMobile(phones)) return null;
	var op = {};
	var host = "cf.51welink.com";
	var msg = "sname=dlmpjkj0&spwd=B4tZJ7Vk&scorpid=&sprdid=1012818&sdst=" +
		phones + "&smsg=" + encodeURIComponent(message);
	var path = "/submitdata/Service.asmx/g_Submit";
	var header = {"Content-Type": "application/x-www-form-urlencoded;charset=utf-8", 'Connection': 'Keep-Alive', 'Content-Length': msg.length};
	op.hostname = host;
	op.path = path;
	op.headers = header;
	op.method = "POST";
	op.port = 80;
	op.data = msg;
	return op;
}
var TOKEN = "menpuji";
function checkSignature(req) {
	var signature = req.query["signature"];
	var timestamp = req.query["timestamp"];
	var nonce = req.query["nonce"];

	var token = TOKEN;
	var tmpArr = [token, timestamp, nonce];
	tmpArr.sort();
	var tmpStr = tmpArr.join('');
	tmpStr = sha1(tmpStr);

	if (tmpStr == signature) {
		return true;
	} else {
		return false;
	}
}
